<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
	href="${pageContext.request.contextPath }/layui/layui_2/layui/css/layui.css"
	media="all">
</head>
<body>
	<br/>
	<button class="layui-btn" id="save_dept_button">
  		<i class="layui-icon">&#xe608;</i>添加
	</button>
	<table class="layui-table" id="dept" lay-filter="dept_table"></table>
	<script src="${pageContext.request.contextPath }/layui/layui_2/layui/layui.js"></script>
	<script>
		layui.use(['form','element','jquery','table','layer'], function() {
			var form = layui.form,
				element = layui.element,
				$ = layui.jquery,
				table = layui.table,
				layer = layui.layer;
			
			var cols = [ [ {
				field : 'p_id',
				align : 'center',
				title : '编号',
				width : 180
			}, {
				field : 'p_name',
				id : 'pName',
				align : 'center',
				title : '部门名称',
				width : 320
			}, {
				field : 'p_remark',
				align : 'center',
				title : '部门备注',
				width : 500
			}, {
				title : '修改',
				align : 'center',
				toolbar : '#update',
				width : 350
			}, {
				title : '撤销部门',
				align : 'center',
				toolbar : '#revoke',
				width : 350
			} ] ];
			
			//方法级渲染
			table.render({
				elem : '#dept',
				url : '${pageContext.request.contextPath }/deptAction_findByPage',
				cols : cols,
				id : 'testReload',
				limit: 10,
			    limits: [10, 30, 50],
				page : true
			});
			
			$('#save_dept_button').click(function(){
				layer.open({
					type: 2,
					title: '添加部门',
					skin: 'layui-layer-molv',
					shadeClose: true,
					shade: 0.5,
					id: 'dept_save',
					area: ['600px', '300px'],
					content: '${pageContext.request.contextPath }/page_dept_add'
				})
			});
			
			//监听工具条
			table.on('tool(dept_table)', function(obj){
			    var data = obj.data;
			    var id = data.p_id;
			    if(obj.event === 'del'){
			        layer.confirm('此操作会导致该部门下所有员工离职!确定要撤销“'+data.p_name+'”吗', {title:"撤销确定"}, function(index){
			        	$.post("${pageContext.request.contextPath}/empAction_findByPid",{p_id : id},function(data){
				         	if(data.result == "ok"){
				         		location.href = "${pageContext.request.contextPath}/deptAction_del?p_id="+id;
				         	}else{
				         		layer.open({
				         			title: '撤销失败',
				         		    content: '该部门下有以下员工存在未交接的客户 "'+data.result+'"请完善客户的交接工作!!'
				         		    ,btn: ['确定']
				         		    ,yes: function(index){
				         		      layer.close(index);
				         		    }
				         		 });
				         	}
				         })
			            layer.close(index);
			        });
			     } else if(obj.event === 'edit'){
			     	layer.open({
						type: 2,
						title: false,
						area: ['600px', '300px'],
						content: '${pageContext.request.contextPath }/deptAction_update_findById?p_id=' + data.p_id
					})
			     }
			 });
		})
		
	</script>
	<script type="text/html" id="update">
		{{#  if(d.p_name != "管理规划部"){ }}
    		<a class="layui-btn layui-btn-mini" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
  		{{#  } else { }}
  			<i class="layui-icon">&#x1007;</i>不可操作
		{{#  } }}
	</script>
	<script type="text/html" id="revoke">
		{{#  if(d.p_name != "管理规划部"){ }}
    		<a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del"><i class="layui-icon">&#x1006;</i>撤销</a>
  		{{#  } else { }}
  			<i class="layui-icon">&#x1007;</i>不可操作
		{{#  } }}
	</script>
</body>
</html>